<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./shopping-car/vue.js"></script>
    <script src="./shopping-car/1.js"></script>
    <style>
        *{margin: 0;padding: 0;}
        h1{
            height: 48px;
            background: palevioletred;
            color: #fff;
            text-align: center;
            line-height: 48px;
            font-size: 26px;
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
        }
        ul{
            margin-top: 48px;
        }
        li{
            list-style: none;
            display: flex;
            padding: 10px 0;
            border-bottom: 1px solid #ccc;
        }
        li>div{
            flex: 1;
        }
        li input{
            margin: 50px 0 0 10px;
        }
        li>div img{
            width: 80px;
            height: 120px;
            margin: 0 20px;
        }
        .shop{
            display: flex;
        }
        .shop div{
            flex: 1;
        }
        .shop-item button{
            width: 20px;
        }
        #all-shop{
            height: 50px;
            background: purple;
            color: #fff;
            position: fixed;
            bottom: 0;
            right: 0;
            left: 0;
            z-index: 100;
        }
    </style>
</head>
<body>
    <div id="app">
        <h1>购物车</h1>
        <div class="shopping-cart">
            <ul>
                <li v-for="(item,index) in list">
                    <input type="checkbox" v-model="item.isChecked">
                    <div class="shop">
                        <img :src="item.show.img" alt="">
                        <div class="shop-item">
                            <h4>{{item.title}}</h4>
                            <p>
                                <span>商品单价：￥{{item.price}}</span>
                                <button @click="item.num++">+</button>
                                <span>{{item.num}}</span>
                                <button @click="numPre(index)">-</button>
                            </p>
                        </div>
                    </div>
                </li>
            </ul>
        </div>
        <div id="all-shop">
            <h3>您一共购买{{allNum}}件商品，商品总价为￥{{allPrice | toFixed}}元
                <button>购买</button>
            </h3>
        </div>
    </div>
    <script>
        let app = new Vue({
            el:'#app',
            data:{
                list:[]
            },
            created() {
                list.data.list.forEach(item => {
                    item.num = 1
                    item.isChecked = true
                })
                this.list = list.data.list
            },
            methods: {
                numPre(index){
                    if(this.list[index].num > 1){
                        this.list[index].num--
                    }
                }
            },
            computed:{
                allNum(){
                    let shopArr = this.list.filter(item => item.isChecked)
                    return shopArr.reduce((a,b) => {
                        return a+b.num
                    },0)
                },
                allPrice(){
                    let shopArr = this.list.filter(item => item.isChecked)
                    return shopArr.reduce((a,b) => {
                        return a+b.num * b.price
                    },0)
                }
            },
            filters:{
                toFixed(v){
                    return v.toFixed(2)
                }
            }
        })
    </script>
</body>
</html>